package cn.yoshii.ad.adserver

import cn.yoshii.ad.adserver.model.entity.AdInfo
import cn.yoshii.ad.adserver.param.CallbackRequest
import cn.yoshii.ad.adserver.param.DeviceRequest
import cn.yoshii.ad.adserver.param.ReportRequest
import cn.yoshii.libs.webstarter.annotation.validate.RequestValidate
import cn.yoshii.libs.webstarter.result.Result
import cn.yoshii.libs.webstarter.result.succeed
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController

/**
 * @author xiang
 * @Date 2024/2/15 20:08
 **/
@RestController
class AdController {

    @Autowired
    private lateinit var adService: AdService

    /**
     * 拉活回调
     */
    @GetMapping("/callback")
    fun callback(request: CallbackRequest): Result {
        adService.callback(request)
        return succeed()
    }

    /**
     * 获取广告
     */
    @GetMapping("/ads")
    @RequestValidate
    fun getAds(request: DeviceRequest): Result {
        val ads = adService.getAds(request)
        return succeed(ads)
    }

    /**
     * 添加广告
     */
    @PostMapping("/ad")
    fun updateAd(@RequestBody adInfo: AdInfo): Result {
        adService.updateAd(adInfo)
        return succeed()
    }

    /**
     * 点击上报
     */
    @PostMapping("/report")
    @RequestValidate
    fun report(@RequestBody request: ReportRequest): Result {
        val requestId = adService.report(request)
        return succeed(requestId)
    }

    /**
     * 获取是否转化
     */
    @GetMapping("/active")
    fun isActive(requestId: String): Result {
        val active = adService.isActive(requestId)
        return succeed(active)
    }

}